package tree;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2024/8/24-下午12:21
 * Since  :   1.0
 * Version:   1.0
 */
public class ArrayTree {
    private int i;
    private  int array[];

    public ArrayTree(int[] array) {
        this.array = array;
    }

    public void preOrder(int i){
        if (array.length==0) {
            System.out.println("the array is null ");
        }
        System.out.println(array[i]);
        int l=i*2+1;
        int r=i*2+2;
        if (l<array.length) {
            preOrder(l);
        }
        if (r<array.length) {
            preOrder(r);
        }

    }

    public void midOrder(int i){
        if (array.length==0) {
            System.out.println("the array is null ");
        }

        int l=i*2+1;
        int r=i*2+2;
        if (l<array.length) {
            midOrder(l);
        }
        System.out.println(array[i]);
        if (r<array.length) {
            midOrder(r);
        }

    }


    public void postOrder(int i){
        if (array.length==0) {
            System.out.println("the array is null ");
        }

        int l=i*2+1;
        int r=i*2+2;
        if (l<array.length) {
            postOrder(l);
        }

        if (r<array.length) {
            postOrder(r);
        }
        System.out.println(array[i]);

    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        ArrayTree arrayTree = new ArrayTree(arr);
        //前序遍历
        arrayTree.preOrder(0); //1	2	4	5	3	6	7
        //中序遍历
        System.out.println();
        arrayTree.midOrder(0);//4	2	5	1	6	3	7
        //后序遍历
        System.out.println();
        arrayTree.postOrder(0);//4	2	5	6	3	7	1

    }



}
